package com.example.shunsu.pojo;

import cn.hutool.core.date.DatePattern;
import com.example.mapper.OracleMapper;
import com.example.pojo.OracleColumn;
import com.example.pojo.OracleTable;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class PojoMain {
    private String ftlPath = "D:\\code\\learning-demo\\common-demo\\src\\main\\resources\\templates\\ftl\\";


    private String toPath = "D:\\work\\dzjj-xf\\com\\zc\\domain\\single\\";

//    private String toPath = "D:\\work\\ubiao\\ubiao-xf\\com\\zc\\domain\\single\\";


    @Autowired
    private OracleMapper oracleMapper;

    public void generatePo() {
        PojoMain pojoMain = new PojoMain();

        Map<String, Object> param = new HashMap<>();
        List<OracleTable> oracleTables = oracleMapper.listTable("");

        for (OracleTable table : oracleTables) {
            String tableName = table.getTableName();
            String comments = table.getComments();
            String lastDdlTime = DatePattern.NORM_DATETIME_FORMAT.format(table.getLastDdlTime());
            System.out.println(lastDdlTime + "-生成表：" + tableName);

            List<OracleColumn> oracleColumns = oracleMapper.listColumn(tableName);


            List<Map<String, Object>> listPk = oracleMapper.getPkByTable(tableName);

            StringBuilder pkName = new StringBuilder();
            for (int i = 0; i < listPk.size(); i++) {
                Map<String, Object> m = listPk.get(i);
                if (i > 0) {
                    pkName.append(",");
                }
                pkName.append("\"").append(m.get("COLUMN_NAME").toString()).append("\"");
            }

            boolean isHasDate = false;
            for (int i = 0; i < oracleColumns.size(); i++) {
                OracleColumn oracleColumn = oracleColumns.get(i);
                if("DATE".equals(oracleColumn.getDataType())){
                    isHasDate = true;
                    break;
                }
            }

            param.put("pkName", pkName.toString());
            param.put("tableName", tableName);
            param.put("comments", comments);
            param.put("lastDdlTime", lastDdlTime);
            param.put("oracleColumns", oracleColumns);
            param.put("isHasDate", isHasDate);

            pojoMain.exe(param, tableName + ".java");
        }
    }

    public void exe(Map<String, Object> root, String fileName) {
        try {


            Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);

            String ftlPath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("templates")).getPath() + "/ftl";
            // 配置模板位置
            cfg.setDirectoryForTemplateLoading(new File(ftlPath));
            cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_26));

            Template temp = cfg.getTemplate("pojo.ftl");

            // 生成文件的位置
            File file = new File(toPath + fileName);
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);

            root.put("cus", new Custom());// 自定义函数
            temp.process(root, bw);
            bw.flush();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
